<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>菜单列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8" />
    <link rel="stylesheet" href="/static/system/css/font.css">
    <link rel="stylesheet" href="/static/system/css/xadmin.css">
    <script src="/static/system/lib/layui/layui.js" type="text/javascript"></script>
    <script src="/static/system/js/xadmin.js" type="text/javascript"></script>
    <!--[if lt IE 9]>
        <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
        <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        .my-center {
            text-align: center !important;
        }

        .my-parent {
            font-weight: bold;
        }
    </style>
</head>

<body>
    <div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">演示</a>
            <a>
                <cite>导航元素</cite></a>
        </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
            onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
    </div>
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body ">
                        <div class="layui-form layui-col-space5">
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn" onclick="menu_add('顶级菜单', 0)"><i
                                        class="layui-icon">&#xe654;</i>添加顶级菜单
                                </button>
                            </div>
                        </div>
                    </div>
                    <div class="layui-card-body layui-table-body layui-table-main">
                        <table class="layui-table layui-form" lay-size="sm">
                            <thead>
                                <tr>
                                    <th class="my-center" style="width: 30px;">菜单ID</th>
                                    <th class="my-center" style="width: 30px;">pid</th>
                                    <th>菜单名称</th>
                                    <th class="my-center" style="width: 30px;">图标</th>
                                    <th class="my-center">模块/控制器/方法</th>
                                    <th style="width: 30px;">排序</th>
                                    <th class="my-center">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {foreach $menuList as $menuParent}
                                <tr>
                                    <td class="my-center">{$menuParent.menu_id}</td>
                                    <td class="my-center">{$menuParent.pid}</td>
                                    <td class="my-parent">{$menuParent.m_name}</td>
                                    <td class="my-center">
                                        <i class="iconfont">{php} echo $menuParent["icon"];{/php}</i>
                                    </td>
                                    <td class="my-center">
                                        --
                                    </td>
                                    <td>{$menuParent.sort}</td>
                                    <td class="my-center">
                                        <a title="编辑"
                                            onclick="menu_upd('{$menuParent.m_name}','{$menuParent.m_name}','{$menuParent.menu_id}','{$menuParent.pid}')"
                                            href="javascript:;">
                                            <i class="layui-icon">&#xe642;</i>
                                        </a>
                                        <a title="删除" onclick="menu_del('{$menuParent.menu_id}')" href="javascript:;">
                                            <i class="layui-icon">&#xe640;</i>
                                        </a>
                                        <a title="添加子菜单"
                                            onclick="menu_add('{$menuParent.m_name}','{$menuParent.menu_id}')"
                                            href="javascript:;">
                                            <i class="layui-icon">&#xe654;</i>
                                        </a>
                                    </td>
                                </tr>
                                {if isset($menuParent.children) }
                                {foreach $menuParent.children as $menuChildren}
                                <tr>
                                    <td class="my-center">{$menuChildren.menu_id}</td>
                                    <td class="my-center">{$menuChildren.pid}</td>
                                    <td>|--{$menuChildren.m_name}</td>
                                    <td class="my-center">
                                        --
                                    </td>
                                    <td class="my-center">{$menuChildren.mca_url}</td>
                                    <td>{$menuChildren.sort}</td>
                                    <td class="my-center">
                                        <a title="编辑"
                                            onclick="menu_upd('{$menuParent.m_name}','{$menuChildren.m_name}','{$menuChildren.menu_id}','{$menuChildren.pid}')"
                                            href="javascript:;">
                                            <i class="layui-icon">&#xe642;</i>
                                        </a>
                                        <a title="删除" onclick="menu_del('{$menuChildren.menu_id}')" href="javascript:;">
                                            <i class="layui-icon">&#xe640;</i>
                                        </a>
                                        <a title="添加子菜单"
                                            onclick="menu_add('{$menuChildren.m_name}','{$menuChildren.menu_id}')"
                                            href="javascript:;">
                                            <i class="layui-icon">&#xe654;</i>
                                        </a>
                                    </td>
                                </tr>
                                {if isset($menuChildren.children) }
                                {foreach $menuChildren.children as $menuChildren2}
                                <tr>
                                    <td class="my-center">{$menuChildren2.menu_id}</td>
                                    <td class="my-center">{$menuChildren2.pid}</td>
                                    <td>&nbsp;&nbsp;|--{$menuChildren2.m_name}</td>
                                    <td class="my-center">
                                        --
                                    </td>
                                    <td class="my-center">{$menuChildren2.mca_url}</td>
                                    <td>{$menuChildren2.sort}</td>
                                    <td class="my-center">
                                        <a title="编辑"
                                            onclick="menu_upd('{$menuChildren.m_name}','{$menuChildren2.m_name}','{$menuChildren2.menu_id}','{$menuChildren2.pid}')"
                                            href="javascript:;">
                                            <i class="layui-icon">&#xe642;</i>
                                        </a>
                                        <a title="删除" onclick="menu_del('{$menuChildren2.menu_id}')"
                                            href="javascript:;">
                                            <i class="layui-icon">&#xe640;</i>
                                        </a>
                                    </td>
                                </tr>
                                {/foreach}
                                {/if}
                                {/foreach}
                                {/if}
                                {/foreach}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        // 添加子菜单
        function menu_add(parent_name, pid) {
            if (pid == 0) {
                xadmin.open(parent_name, "{:url('menuAdd')}?pid=" + pid, 600, 400);
            } else {
                xadmin.open(parent_name + "->添加子菜单", "{:url('menuAdd')}?pid=" + pid, 600, 400);
            }
        }

        // 修改菜单
        function menu_upd(parent_name, m_name, menu_id, pid) {
            if (pid == 0) {
                xadmin.open(m_name, "{:url('menuUpd')}?menu_id=" + menu_id + "&pid=" + pid, 600, 400);
            } else {
                xadmin.open(parent_name + "->" + m_name, "{:url('menuUpd')}?menu_id=" + menu_id + "&pid=" + pid, 600, 400);
            }
        }

        // 删除菜单
        function menu_del(menu_id) {
            layer.confirm('确认要删除吗？', function (index) {
                //发异步删除数据
                $.ajax({
                    url: "{:url('menuDel')}",
                    type: "POST",
                    data: {
                        menu_id: menu_id
                    },
                    success: function (res) {
                        if (res.code == 0) {
                            layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                                location.reload(); //刷新父页面
                            });
                        } else {
                            layer.alert(res.msg, { icon: 5 });
                        }
                    }
                });
            });
        }
    </script>
</body>

</html>